package com.meituan.catering.management.product.dao.converter;

import com.google.common.collect.Lists;
import com.meituan.catering.management.product.biz.model.request.CreateProductBizRequest;
import com.meituan.catering.management.product.biz.model.request.UpdateProductBizRequest;
import com.meituan.catering.management.product.dao.model.ProductAccessoryDO;
import com.meituan.catering.management.product.dao.model.ProductDO;
import com.meituan.catering.management.product.dao.model.ProductMethodDO;
import org.apache.commons.collections4.CollectionUtils;

import java.util.Collections;
import java.util.Date;
import java.util.List;

/**
 * @author Believe
 * Date 2022/5/23 9:42
 */
public class ProductDOConverter {
    public static ProductDO toProductDO(Long tenantId, Long userId, CreateProductBizRequest createProductBizRequest) {
        ProductDO productDO = new ProductDO();
        productDO.setName(createProductBizRequest.getName());
        productDO.setUnitPrice(createProductBizRequest.getUnitPrice());
        productDO.setUnitOfMeasure(createProductBizRequest.getUnitOfMeasure());
        productDO.setMinSalesQuantity(createProductBizRequest.getMinSalesQuantity());
        productDO.setIncreaseSalesQuantity(createProductBizRequest.getIncreaseSalesQuantity());
        productDO.setDescription(createProductBizRequest.getDescription());
        productDO.setEnabled(Boolean.TRUE);
        productDO.setTenantId(tenantId);
        productDO.setCreatedBy(userId);
        productDO.setCreatedAt(new Date());
        productDO.setLastModifiedBy(userId);
        productDO.setLastModifiedAt(new Date());
        return productDO;
    }

    public static ProductDO toProductDO(Long tenantId, Long userId, Long id, UpdateProductBizRequest updateProductBizRequest) {
        ProductDO productDO = new ProductDO();
        productDO.setName(updateProductBizRequest.getName());
        productDO.setUnitPrice(updateProductBizRequest.getUnitPrice());
        productDO.setUnitOfMeasure(updateProductBizRequest.getUnitOfMeasure());
        productDO.setMinSalesQuantity(updateProductBizRequest.getMinSalesQuantity());
        productDO.setIncreaseSalesQuantity(updateProductBizRequest.getIncreaseSalesQuantity());
        productDO.setDescription(updateProductBizRequest.getDescription());
        productDO.setVersion(updateProductBizRequest.getVersion());
        productDO.setId(id);
        productDO.setEnabled(Boolean.TRUE);
        productDO.setTenantId(tenantId);
        productDO.setCreatedBy(userId);
        productDO.setCreatedAt(new Date());
        productDO.setLastModifiedBy(userId);
        productDO.setLastModifiedAt(new Date());
        return productDO;
    }


    public static List<ProductAccessoryDO> toCreateAccessoryDO(Long tenantId, List<CreateProductBizRequest.AccessoryGroup> accessoryGroups) {
        if (CollectionUtils.isEmpty(accessoryGroups)) {
            return Collections.emptyList();
        }
        List<ProductAccessoryDO> productAccessoryDOList = Lists.newLinkedList();
        for (CreateProductBizRequest.AccessoryGroup accessoryGroup : accessoryGroups) {
            for (CreateProductBizRequest.AccessoryGroup.Option option : accessoryGroup.getOptions()) {
                ProductAccessoryDO productAccessoryDO = new ProductAccessoryDO();
                productAccessoryDO.setTenantId(tenantId);
                productAccessoryDO.setGroupName(accessoryGroup.getName());
                productAccessoryDO.setName(option.getName());
                productAccessoryDO.setUnitPrice(option.getUnitPrice());
                productAccessoryDO.setUnitOfMeasure(option.getUnitOfMeasure());
                productAccessoryDOList.add(productAccessoryDO);
            }
        }
        return productAccessoryDOList;
    }


    public static List<ProductAccessoryDO> toUpdateAccessoryDO(Long tenantId, Long productId, List<UpdateProductBizRequest.AccessoryGroup> accessoryGroups) {
        if (CollectionUtils.isEmpty(accessoryGroups)) {
            return Collections.emptyList();
        }
        List<ProductAccessoryDO> productAccessoryDOList = Lists.newLinkedList();
        for (UpdateProductBizRequest.AccessoryGroup accessoryGroup : accessoryGroups) {
            for (UpdateProductBizRequest.AccessoryGroup.Option option : accessoryGroup.getOptions()) {
                ProductAccessoryDO productAccessoryDO = new ProductAccessoryDO();
                productAccessoryDO.setProductId(productId);
                productAccessoryDO.setTenantId(tenantId);
                productAccessoryDO.setId(option.getId());
                productAccessoryDO.setGroupName(accessoryGroup.getName());
                productAccessoryDO.setName(option.getName());
                productAccessoryDO.setUnitPrice(option.getUnitPrice());
                productAccessoryDO.setUnitOfMeasure(option.getUnitOfMeasure());
                productAccessoryDOList.add(productAccessoryDO);
            }
        }
        return productAccessoryDOList;
    }

    public static List<ProductMethodDO> toCreateMethodDO(Long tenantId, Long productId, List<CreateProductBizRequest.MethodGroup> groupList) {
        if (CollectionUtils.isEmpty(groupList)) {
            return Collections.emptyList();
        }
        List<ProductMethodDO> productMethodDOS = Lists.newLinkedList();
        for (CreateProductBizRequest.MethodGroup methodGroup : groupList) {
            for (CreateProductBizRequest.MethodGroup.Option option : methodGroup.getOptions()) {
                ProductMethodDO productMethodDO = new ProductMethodDO();
                productMethodDO.setProductId(productId);
                productMethodDO.setTenantId(tenantId);
                productMethodDO.setGroupName(methodGroup.getName());
                productMethodDO.setName(option.getName());
                productMethodDOS.add(productMethodDO);
            }
        }
        return productMethodDOS;
    }

    public static List<ProductMethodDO> toUpdateMethodDO(Long tenantId, Long productId, List<UpdateProductBizRequest.MethodGroup> methodBOList) {
        if (CollectionUtils.isEmpty(methodBOList)) {
            return Collections.emptyList();
        }
        List<ProductMethodDO> productMethodDOS = Lists.newLinkedList();
        for (UpdateProductBizRequest.MethodGroup methodGroup : methodBOList) {
            for (UpdateProductBizRequest.MethodGroup.Option option : methodGroup.getOptions()) {
                ProductMethodDO productMethodDO = new ProductMethodDO();
                productMethodDO.setProductId(productId);
                productMethodDO.setId(option.getId());
                productMethodDO.setTenantId(tenantId);
                productMethodDO.setGroupName(methodGroup.getName());
                productMethodDO.setName(option.getName());
                productMethodDOS.add(productMethodDO);
            }
        }
        return productMethodDOS;
    }
}
